package com.hula.onion.book.server.dao.impl;

import com.hula.onion.book.server.dao.IPlatformInfoDao;
import com.hula.onion.book.server.vo.PlatformInfoVo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

/**
 * Created by shan on 18/1/24.
 */
@Repository
public class PlatformInfoDaoImpl extends JdbcDaoSupport implements IPlatformInfoDao {
    @Resource(name = "dataSource")
    private DataSource dataSource;

    PlatformInfoDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
    }

    private static final BeanPropertyRowMapper<PlatformInfoVo> mapper =
            new BeanPropertyRowMapper<>(PlatformInfoVo.class);

    private static final String GET_PLATFORM = "select * from platform_info ";

    @Override
    public List<PlatformInfoVo> getPlatformList(String platformName) {
        List<PlatformInfoVo> result = null;
        if (null != platformName) {
            result = this.getJdbcTemplate().query(GET_PLATFORM + " where name like ? ", new Object[]{
                    "%" + platformName + "%"}, mapper);
        } else {
            result = this.getJdbcTemplate().query(GET_PLATFORM, mapper);
        }
        return result;

    }

    @Override
    public PlatformInfoVo getPlatformById(int id) {

        return this.getJdbcTemplate().query(GET_PLATFORM + "where id = ?", new Object[]{id}, mapper).get(0);
    }
}
